package graphisme;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class PaperRockGrapicalInterface extends JFrame implements
		ActionListener {

	private static final long serialVersionUID = 1L;

	// window
	JPanel window;

	// score label
	JLabel labelYourScore = new JLabel("Score : 0");
	JLabel labelOpponentScore = new JLabel("Score : 0");

	// score
	int yourScore = 0;
	int opponentScore = 0;

	// images
	Image imgRock;;
	Image imgPaper;
	Image imgScissors;
	Image imgLizard;
	Image imgSpock;
	
	JFrame f = new JFrame();

	// buttons
	JButton buttonRock = new JButton();
	JButton buttonPaper = new JButton();
	JButton buttonScissor = new JButton();
	JButton buttonLizard = new JButton();
	JButton buttonSpock = new JButton();

	// communication part
	Socket socket = null;
	OutputStream out;
	BufferedWriter bufOut = null;

	boolean yourTurn = true;

	public PaperRockGrapicalInterface(Socket socket) throws IOException {
		this.socket = socket;
		this.out = socket.getOutputStream();
		this.bufOut = new BufferedWriter(new OutputStreamWriter(out));
		window = new JPanel();

		// window's size
		this.setSize(600, 900);

		// window's title
		this.setTitle("Rock-Paper-Scissors-Lizard-Spock");

		// add labels
		window.add(labelYourScore);
		window.add(labelOpponentScore);

		// labels' position
		labelYourScore.setLocation(0, 0);
		labelOpponentScore.setLocation(0, 0);

		// create buttons withs images
		buttonPaper.setIcon(new ImageIcon("src/graphisme/Images/paper.png"));
		buttonRock.setIcon(new ImageIcon("src/graphisme/Images/rock.png"));
		buttonScissor
				.setIcon(new ImageIcon("src/graphisme/Images/scissors.png"));
		buttonLizard.setIcon(new ImageIcon("src/graphisme/Images/lizard.png"));
		buttonSpock.setIcon(new ImageIcon("src/graphisme/Images/spock.jpg"));

		// add buttons' event handler
		buttonPaper.addActionListener(this);
		buttonRock.addActionListener(this);
		buttonScissor.addActionListener(this);
		buttonLizard.addActionListener(this);
		buttonSpock.addActionListener(this);

		// add buttons to window
		window.add(buttonRock);
		window.add(buttonPaper);
		window.add(buttonScissor);
		window.add(buttonLizard);
		window.add(buttonSpock);
		this.add(window);
	}

	// send message to other player
	public void sendMsg(String msg) {
		try {
			this.bufOut.write(msg);
			this.bufOut.newLine();
			this.bufOut.flush();
		} catch (IOException e) {
			System.out
					.println("An error has been occured while sending the message to the server");
			e.printStackTrace();
		}
	}

	// update score
	public void setlabelYourScore(int x) {
		this.yourScore += x;
		System.out.println("----" + yourScore);
		this.labelYourScore.setText("Score : " + yourScore);
	}

	public void setlabelOpponentScore(int x) {
		this.opponentScore += x;
		System.out.println(opponentScore);
		this.labelOpponentScore.setText("Score : " + opponentScore);
	}

	public boolean isYourTurn() {
		return yourTurn;
	}

	public void setYourTurn(boolean yourTurn) {
		this.yourTurn = yourTurn;
	}

	// buttons click handler
	@Override
	public void actionPerformed(ActionEvent event) {
		if (isYourTurn()) {
			// if the player clicks on the paper button
			if (event.getSource() == buttonPaper) {
				sendMsg("Paper");
			}
			// if the player clicks on the rock button
			else if (event.getSource() == buttonRock) {
				sendMsg("Rock");

			}
			// if the player clicks on the scissors button
			else if (event.getSource() == buttonScissor) {
				sendMsg("Scissors");

			}
			// if the player clicks on the lizard button
			else if (event.getSource() == buttonLizard) {
				sendMsg("Lizard");

			}
			// if the player clicks on the spock button
			else if (event.getSource() == buttonSpock) {
				sendMsg("Spock");

			}
			setYourTurn(false);
		}
		else{
			JOptionPane.showMessageDialog(

					f, "You have already played", "Information", JOptionPane.PLAIN_MESSAGE);
		}
	}
}
